/************************************************************
 *  * Hyphenate CONFIDENTIAL
 * __________________
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 *
 * NOTICE: All information contained herein is, and remains
 * the property of Hyphenate Inc.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Hyphenate Inc.
 */

#import "UserProfileViewController.h"

#import "ChatViewController.h"
#import "UserProfileManager.h"
#import "UIImageView+HeadImage.h"
#import "XXBUserInfo.h"

@interface UserProfileViewController ()

@property (strong, nonatomic) UserProfileEntity *user;

@property (strong, nonatomic) UIImageView *headImageView;
@property (strong, nonatomic) UILabel *usernameLabel;
@property (nonatomic,strong) NSDictionary *userInfo;

@end

@implementation UserProfileViewController

- (instancetype)initWithUsername:(NSString *)username
{
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        _username = username;
    }
    return self;

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"个人信息";
    
    self.view.backgroundColor = [UIColor colorWithRed:0.88 green:0.88 blue:0.88 alpha:1.0];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.allowsSelection = NO;
    
    [self setupBarButtonItem];
    [self loadUserProfile];
}

- (UIImageView*)headImageView
{
    if (!_headImageView) {
        _headImageView = [[UIImageView alloc] init];
        _headImageView.frame = CGRectMake(20, 10, 60, 60);
        _headImageView.contentMode = UIViewContentModeScaleToFill;
    }
    
    NSString *imageStr = [NSString stringWithFormat:@"%@",[_userInfo objectForKey:@"photo"]];
    
    if ([_username isEqualToString:[EMClient sharedClient].currentUsername]) {
           [_headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",url_Base,imageStr]] placeholderImage:[UIImage imageNamed:@"Icon-40.png"]];
    }else{
           [_headImageView sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage imageNamed:@"Icon-40.png"]];
    }

    return _headImageView;
}

- (UILabel*)usernameLabel
{
    if (!_usernameLabel) {
        _usernameLabel = [[UILabel alloc] init];
        _usernameLabel.frame = CGRectMake(CGRectGetMaxX(_headImageView.frame) + 10.f, 10, 200, 20);
        _usernameLabel.text = _username;
        _usernameLabel.textColor = [UIColor lightGrayColor];
    }
    return _usernameLabel;
}

#pragma mark - Table view datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    if (indexPath.row == 0) {
        UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];
        lineLabel.backgroundColor = RGB(220, 220, 220);
        [cell.contentView addSubview:lineLabel];
        cell.detailTextLabel.text = @"头像";
        [cell.contentView addSubview:self.headImageView];
    } else if (indexPath.row == 1) {
        cell.textLabel.text = @"个人账 户";
        cell.detailTextLabel.text = self.usernameLabel.text;
    } else if (indexPath.row == 2) {
        cell.textLabel.text = @"昵称";
        NSString *telPhone = [NSString stringWithFormat:@"%@",[_userInfo objectForKey:@"hnLoginName"]];
        NSString *nickName = [NSString stringWithFormat:@"%@",[_userInfo objectForKey:@"nickname"]];
         cell.detailTextLabel.text = nickName?nickName:telPhone;
        
//        UserProfileEntity *entity = [[UserProfileManager sharedInstance] getUserProfileByUsername:_username];
//        if (entity && entity.nickname.length>0) {
//            cell.detailTextLabel.text = entity.nickname;
//        } else {
//            cell.detailTextLabel.text = _username;
//        }
//        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return cell;
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 80;
    }
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)setupBarButtonItem
{
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 10, 25, 25)];
    [backButton setImage:[UIImage imageNamed:@"xiangyou.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    [self.navigationItem setLeftBarButtonItem:backItem];
}

- (void)loadUserProfile
{
    
    if ([_username isEqualToString:[EMClient sharedClient].currentUsername]) {
        _userInfo = [AppConfig getUserInfo];
    }else{
        _userInfo = [AppConfig getPeopleInfo:_username];
    }
//    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",nil];
//    
//    NSLog(@"param = %@",param);
//    
//    
//    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
//    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//    [manager POST:url_userInfo_detail parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
//        
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSError * error;
//        NSLog(@"responseObject = %@",responseObject);
//        
//        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
//        [self hideHud];
//        if (error) {
//            NSLog(@"====error1:%@",error);
//            [self showHint:@"获取用户信息失败"];
//            NSLog(@"==dict:%@",data);
//            return;
//        }
//        NSLog(@"==dict:%@",data);
//        
//        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
//        if ([result isEqualToString:@"1"]) {
//            _userInfo = [NSDictionary dictionaryWithDictionary:[data objectForKey:@"obj"]];
//            NSLog(@"------obj = %@",_userInfo);
//      
//            
//            
//          
//            
//
//            
//        }else{
//            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
//            [self showHint:info];
//        }
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
//        NSLog(@"====error:%@",error);
//        [self hideHud];
//        [self showHint:@"获取用户信息失败"];
//    }];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
